/**
 * Created by Administrator on 2014/12/3.
 */
var express = require('express')
    , routes = require('./routes')
    , http = require('http')
    , path = require('path')
    , ejs = require('ejs');
var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.engine('.html', ejs.__express);
app.set('view engine', 'html');// app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser({uploadDir:'./public/files'}));
app.use(express.methodOverride());
app.use(express.cookieParser('likeshan'));
app.use(express.session({ secret: "andylau" }));

app.use(function(req, res, next){
    res.locals.user = req.session.user;
    var err = req.session.error;
    delete req.session.error;
    res.locals.message = '';
    if (err) res.locals.message = err;//'<div class="alert alert-error">' + err + '</div>'
    next();
});

app.use(app.router);//注意这个放置的顺序很重要，不要放在session之前，要放在session之后，否则会提示cannot set property 'name' of undefined
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}
var server = http.createServer(app);
server.listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});
module.exports = {
    server : server,
    app: app
}